<template>
    <el-card class="form-container" shadow="never">
        <el-form :model="orderSetting"
             ref="orderSettingForm"
             :rules="rules"
             label-width="150px">
            <el-form-item label="正常订单超过：" prop="normalOrderOvertime">
                <el-input v-model="orderSetting.normalOrderOvertime" class="input-width">
                <template slot="append">分</template>
                </el-input>
                <span class="note-margin">未付款，订单自动关闭</span>
            </el-form-item>
            <el-form-item label="发货超过：" prop="confirmOvertime">
                <el-input v-model="orderSetting.confirmOvertime" class="input-width">
                <template slot="append">天</template>
                </el-input>
                <span class="note-margin">未收货，订单自动收货</span>
            </el-form-item>
            <el-form-item label="退货进行超过：" prop="finishOvertime">
                <el-input v-model="orderSetting.finishOvertime" class="input-width">
                <template slot="append">天</template>
                </el-input>
                <span class="note-margin">未收货，自动完成退货订单</span>
            </el-form-item>
            <el-form-item label="订单收货超过：" prop="commentOvertime">
                <el-input v-model="orderSetting.commentOvertime" class="input-width">
                <template slot="append">天</template>
                </el-input>
                <span class="note-margin">未收货，订单自动关闭</span>
            </el-form-item>
            <el-form-item>
                <el-button
                @click="confirm('orderSettingForm')"
                type="primary">提交</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
import { mapState, mapActions } from 'vuex';
  const checkTime = (rule, value, callback) => {
    if (!value) {
      return callback(new Error('时间不能为空'));
    }
    console.log("checkTime",value);
    let intValue = parseInt(value);
    if (!Number.isInteger(intValue)) {
      return callback(new Error('请输入整数值'));
    }
    callback();
  }
export default ({
    name:'OrderSetting',
    data() {
        return{
            rules: {
            flashOrderOvertime:{validator: checkTime, trigger: 'blur' },
            normalOrderOvertime:{validator: checkTime, trigger: 'blur' },
            confirmOvertime: {validator: checkTime, trigger: 'blur' },
            finishOvertime: {validator: checkTime, trigger: 'blur' },
            commentOvertime:{validator: checkTime, trigger: 'blur' }
            }
        }
    },
    methods:{
        ...mapActions(['fetchOrderSettings']),
        confirm(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否要提交修改?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.$axios.post(this.$httpUrl+'/ordersetting/update',this.orderSetting).then(res=>{
                console.log(res)
                this.$message({
                  type: 'success',
                  message: '提交成功!',
                  duration:1000
                });
              })
            });
          } else {
            this.$message({
              message: '提交参数不合法',
              type: 'warning'
            });
            return false;
          }
        });
      },
    },
    created(){
      this.$store.dispatch('fetchOrderSettings', this.$httpUrl);
    },
    computed:{
      ...mapState({
        orderSetting: state => state.orderSettings
      })
    },
})
</script>
<style scoped>
  .input-width {
    width: 50%;
  }

  .note-margin {
    margin-left: 15px;
  }
</style>
